package com.chixing.day17_reflect_thread.proxy.cglibproxy;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/*
    1.生成代理对象
    2.实现增强
*/
public class OrderProxy implements MethodInterceptor {//方法拦截器
    private Object target;


    public Object getProxy(Object target){
        this.target = target;
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(target.getClass());// target: 目标对象 父类 ， enhancer: 代理对象 子类
        enhancer.setCallback(this);//回调
        return enhancer.create();// enhancer 就是 代理对象（父类就是 目标对象）
    }
    @Override
    public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
        Object result = method.invoke(target,args);
        if (method.getName().equals("save")|| method.getName().equals("update")|| method.getName().equals("delete")){
            new LogUtil().addLog();//  增强实现
        }
        return result;
    }
}
